package com.wuhan.template.module.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseApplication;
import com.wuhan.template.module.home.entity.HomeDetail;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/10 0010.
 */
public class HomeRecyclerAdapter extends RecyclerView.Adapter<HomeRecyclerAdapter.MyViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private ImageLoader loader;
    private List<HomeDetail.ContentBean> contents;
    private DisplayImageOptions options;

    public HomeRecyclerAdapter(Context context, List<HomeDetail.ContentBean> contents) {
        this.context = context;
        loader = ((BaseApplication) (context.getApplicationContext())).getImageLoader();
        this.contents = contents;
        inflater = LayoutInflater.from(this.context);
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .displayer(new RoundedBitmapDisplayer(10))
                .build();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.home_activity_item, null);
        MyViewHolder holder = new MyViewHolder(view);
        holder.homeImage = (ImageView) view.findViewById(R.id.home_itemImage);
        holder.homeTitle = (TextView) view.findViewById(R.id.home_itemTitle);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        HomeDetail.ContentBean contentBean = contents.get(position);
        holder.homeTitle.setText(contentBean.getTitle());
        List<Map<String, String>> childs_data = (List<Map<String, String>>) contentBean.getChilds_data();
        Map<String, String> map = childs_data.get(0);
        String imageUrl = map.get("host") + map.get("dir") + map.get("filepath") + map.get("filename");
        holder.homeImage.setScaleType(ImageView.ScaleType.FIT_XY);
        loader.displayImage(imageUrl, holder.homeImage,options);
        holder.itemView.setTag("");

    }

    @Override
    public int getItemCount() {
        return contents.size();
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , String data);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public MyViewHolder(View itemView) {
            super(itemView);
        }

        ImageView homeImage;
        TextView homeTitle;
    }

}
